<?php
namespace App\Admin\Controllers\FileModel;
use App\Http\Controllers\Controller;
use Dcat\Admin\FormStep\Form as StepForm;
use App\Models\InputStepForm;
use App\Models\InformationList;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
use Dcat\Admin\Grid;
use Illuminate\Support\Facades\DB;
use Log;
// use Dcat\Admin\Widgets\Form;
class ProjectRegisterController extends Controller
{
    //安全检修牌挂、摘牌项目登记 云之家
    protected $options = [
        1 => '显示文本框',
        2 => '显示编辑器',
        3 => '显示文件上传',
        4 => '还是显示文本框',
    ];
    // use PreviewCode;
    public function index(Content $content)
    {
        return $content
            // ->body('<div style="margin:5px 0 15px;">'.$this->buildPreviewButton().'</div>')
            ->body($this->form())
            ->header('安全检修牌挂、摘牌项目登记')
            ->description('分步表单');
    }
    protected function form()
{
    Admin::js('/stepform/js/jquery.smartWizard.min.js');
    Admin::css('/stepform/css/step.css');
    return Form::make(new InputStepForm(), function (Form $form) {
            $form->title('安全检修牌挂、摘牌项目登记');
            $form->action('ProjectRegisterstep');
            $form->disableListButton();
            $form->multipleSteps()
                ->remember() 
                ->width('100%')
                ->add('信息录入', function (StepForm $step) use ($form) {
                    $step->text('pa1',trans('挂牌负责人'))->required();
                    $step->text('pa2',trans('挂牌编号'))->required();
                    // $step->text('pa3',trans('挂牌位置'))->required();
                    // $step->text('pa5',trans('挂牌点'))->required();
                    $step->text('pa4',trans('记录编号'))->required();
                    $step->table('pa6', trans('项目登记'),function ($table) {
                        $table->text('order','委托单号')->width(10, 2)->required();
                        $table->text('pname','检修项目名称')->required();
                        $table->text('count','挂牌总数')->required();
                        $table->text('no','第几块')->required();
                        // $table->text('istrue','点检确认')->required();
                        // $table->text('istrues','生产/操作确认')->required();
                        // $table->text('stime','项目开始时间')->required();
                        // $table->text('name','检修签名')->required();
                        // $table->text('etime','项目结束时间')->required();
                        // $table->text('names','检修签名')->required();
                    })->width(12,0)->rules('required');
                    // $step->multipleImage('jdzp', trans('进度照片'))->autoUpload()->url('step/files');
                });
        });
    }

    public function step(Request $request){
        $info = $request->all();
        if(empty($info['CURRENT_VALIDATION_STEP'])&&$info['ALL_STEPS']==1){//提交
            Log::info($info);
            //实例化 phpword 类
            $PHPWord = new PhpWord();
            //指定事先制作好的模板文件路径
            $templateProcessor = new TemplateProcessor(public_path("filemodel/project_register.docx"));
            if(isset($info['pa6'])){
                $rowsa=count($info['pa6']);//总行数
                $templateProcessor->cloneRow('num',$rowsa);//复制行
                $pa1 = $info['pa6'];
                $pa1 = array_values($pa1);
                for($a=0;$a<$rowsa;$a++){
                    $templateProcessor->setValue("num#".($a+1),$a+1);//替换变量
                    $templateProcessor->setValue("order#".($a+1),$pa1[$a]['order']);//替换变量
                    $templateProcessor->setValue("pname#".($a+1),$pa1[$a]['pname']);
                    $templateProcessor->setValue("count#".($a+1),$pa1[$a]['count']);
                    $templateProcessor->setValue("no#".($a+1),$pa1[$a]['no']);
                }
            }
           
            $templateProcessor->setValue('pa1',$info['pa1']);
            $templateProcessor->setValue('pa2',$info['pa2']);
            // $templateProcessor->setValue('pa3',$info['pa3']);
            $templateProcessor->setValue('pa4',$info['pa4']);
            // $templateProcessor->setValue('pa5',$info['pa5']);

            
            unset($info['_file_']);
            unset($info['CURRENT_VALIDATION_STEP']);
            unset($info['ALL_STEPS']);
            unset($info['_token']);
            $data['admin_id'] = Admin::user()->id;
            $data['name'] = '安全检修牌挂、摘牌项目登记'.date('Y-m-d H:i');
            $data['data'] = $info;
            // $data['content'] = json_encode($info);
            // $insertData['content'] = json_encode($data);
            $insertData['name'] = $data['name'];
            $insertData['type'] = 9;
            $insertData['admin_id'] = Admin::user()->id;
            $insertData['created_at'] = date('Y-m-d H:i:s');
            // $templateProcessor->saveAs(public_path('filemodellist/'.$info['projectname'].md5(time()) . ".docx"));
            $templateProcessor->saveAs(public_path('filemodellist/'.$data['name'].'.docx'));
            InformationList::insert($insertData);
            return $this->form()->saving(function (Form $form) {
                // 清空缓存
                $form->multipleSteps()->flushStash();
                // 拦截保存操作
                return response(
                    $form->multipleSteps()
                        ->done()
                        ->render()
                );
            })->store();
        }
    }

    /**
     * 保存
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function store(Request $requset)
    {
        return $this->form()->saving(function (Form $form) {
            
            // 清空缓存
            $form->multipleSteps()->flushStash();
            // 拦截保存操作
            return response(
                $form->multipleSteps()
                    ->done()
                    ->render()
            );
        })->store();
    }

    public function list($id,Content $content){
        return $content
            ->header('表格列表')
            ->description()
            ->body($this->grid($id));
    }
    protected function grid($typeid)
    {
        return new Grid(InformationList::where(['admin_id'=>Admin::user()->id,'type'=>$typeid]), function (Grid $grid) {
            $grid->showActions();
            $grid->disableDeleteButton();
            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->fixColumns(2);
            
            $grid->column('id', 'ID');
            $grid->column('name', '名称');
            $grid->column('type','类型')->display(function($type) {
                return Db::table('xfy_info_model')->where(['id'=>$type])->value('name');;
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $id = $actions->getKey();
                $edit_url = admin_base_path('downloadfile/'.$id);
                $actions->append("<a href='$edit_url'>下载</a>");
            });
        });
    }
}